﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Media;
using System.Windows.Media;
using Styx.Common;
using Styx.Plugins;
using Styx.WoWInternals;

namespace AlishaInvite
{

    public class AlishaInvite : HBPlugin
    {
        public override string Name { get { return "AlishaInvite"; } }
        public override string Author { get { return "Alisha"; } }
        public override Version Version { get { return new Version(2, 0); } }
		
        public override bool WantButton { get { return true; } }
		public override string ButtonText { get { return "Settings"; } }
		
        private static string SoundFileInvite { get { return Path.Combine(FolderPath + "\\Sounds", "invite.wav"); }}
        private readonly AlishaInviteSettings _alishaSettings = AlishaInviteSettings.Instance;
		
		private readonly Random _r = new Random();
		int _myRndValue;
		
		public int GetNewRandValue { get { return _r.Next((_alishaSettings.MinWaitTimer*1000), (_alishaSettings.MaxWaitTimer*1000)); } }
		
		public override void OnButtonPress()
        {
            new FormSettings().ShowDialog();
        }
		
        static public string FolderPath
        {
            get
            {
                string sPath = Process.GetCurrentProcess().MainModule.FileName;
                return Path.GetDirectoryName(sPath) != null
                            // ReSharper disable AssignNullToNotNullAttribute
                           ? Path.Combine(Path.GetDirectoryName(sPath), "Plugins\\AlishaInvite\\")
                            // ReSharper restore AssignNullToNotNullAttribute
                           : "";
            }
        }

        public override void OnEnable()
        {
            Lua.Events.AttachEvent("LFG_PROPOSAL_SHOW", LfgInvite);
			Lua.Events.AttachEvent("LFG_ROLE_CHECK_SHOW", RolePollBegin);
            Lua.Events.AttachEvent("READY_CHECK", ReadyCheck);
            Lua.Events.AttachEvent("QUEST_DETAIL", QuestAccept);
            Lua.Events.AttachEvent("RESURRECT_REQUEST", ResurrectAccept);
            Log("Loaded Version " + Version);
        }

        public override void OnDisable()
        {
            Lua.Events.DetachEvent("LFG_PROPOSAL_SHOW", LfgInvite);
            Lua.Events.DetachEvent("LFG_ROLE_CHECK_SHOW", RolePollBegin);
            Lua.Events.DetachEvent("READY_CHECK", ReadyCheck);
            Lua.Events.DetachEvent("QUEST_DETAIL", QuestAccept);
            Lua.Events.DetachEvent("RESURRECT_REQUEST", ResurrectAccept);
            Log("Finished");
        }

        public override void Pulse() { }

        #region Lua Events Functions

        public void LfgInvite(object sender, LuaEventArgs args)
        {
            if (!_alishaSettings.AcceptLfgInvite) return;

            if (_alishaSettings.PlaySounds)
                new SoundPlayer(SoundFileInvite).Play();
            _myRndValue = GetNewRandValue;
            Log("Got Dungeon invite, Entering in " + (_myRndValue/1000) + " seconds");
            Thread.Sleep(_myRndValue);
            Lua.DoString("RunMacroText(\"/click LFGDungeonReadyDialogEnterDungeonButton\");");
            Lua.DoString("/script AcceptProposal();");
        }
		
		public void RolePollBegin(object sender, LuaEventArgs args)
		{
            if (!_alishaSettings.AcceptRollPoll) return;

			_myRndValue = GetNewRandValue;
            Log("Roll Poll has begun, Accepting in " + (_myRndValue / 1000) + " seconds");
			Thread.Sleep(_myRndValue);
			Lua.DoString("RunMacroText(\"/click LFDRoleCheckPopupAcceptButton\");");
			Lua.DoString("/script CompleteLFGRoleCheck(true);");
		}
		
        public void ReadyCheck(object sender, LuaEventArgs args)
        {
            if (!_alishaSettings.AcceptReadyChecks) return;

			_myRndValue = GetNewRandValue;
            Log("Got Ready Check, clicking Ready Button in " + (_myRndValue / 1000) + " seconds");
            Thread.Sleep(_myRndValue);
            Lua.DoString("RunMacroText(\"/click ReadyCheckFrameYesButton\");");
        }
        public void QuestAccept(object sender, LuaEventArgs args)
        {
            if (!_alishaSettings.AcceptSharedQuests) return;

			//_myRndValue = GetNewRandValue;
            Log("Got Quest Dialog, clicking Accept Button in 1 second");
            Thread.Sleep(1000);
            Lua.DoString("RunMacroText(\"/click QuestFrameAcceptButton\";");
        }
        public void ResurrectAccept(object sender, LuaEventArgs args)
        {
            if (!_alishaSettings.AcceptResurrect) return;

			_myRndValue = GetNewRandValue;
            Log("Got Resurrect Request, clicking Accept Button in " + (_myRndValue / 1000) + " seconds");
            Thread.Sleep(_myRndValue);
            Lua.DoString("RunMacroText(\"/click StaticPopup1Button1\");");
        }
		
        #endregion

        #region Logging

        public static void Log(string msg)
        {
            Logging.Write(Colors.Aqua, "[AlishaInvite]: " + msg);
        }

        #endregion

    }
}